#include <iostream>
using namespace std;

int LastNumber(int n, int m)
{
	int ans = 0;
	for (int i = 2; i <= n; i++)
	{
		ans = (ans + m) % i;
	}
	return ans;
}


int main()
{
	cout<<LastNumber(5,3) << endl;
	return 0;
}